﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.7.0.1
 * 13 Dec 2011
 **/
namespace Gemina.CRM2011.WF.LdgManager
{
    abstract public class AgDeployView : AgCodeActivityBase
    {

        #region Constants

        private const string Param1OutputViewId = "Output View Id";
        private const string Param2OutputResultAction = "Result Action";

        #endregion

        #region Properties

        public int? ValidatedQueryType { get; set; }
        public ActionEnum ValidatedAction { get; set; }
        public string ValidatedReturnedType { get; set; }
        public string ValidatedDescription { get; set; }
        public string ValidatedFetchXml { get; set; }
        public string ValidatedLayoutXml { get; set; }
        public string ValidatedViewDefinition { get; set; }
        public string ValidatedViewId { get; set; }
        public string ValidatedQueryName { get; set; }

        #endregion

        #region Workflow Output Parameters

        /// <summary>
        /// Output parameter
        /// </summary>
        [Output(Param1OutputViewId)]
        public OutArgument<String> OutputViewId { get; set; }

        /// <summary>
        /// Output parameter
        /// </summary>
        [Output(Param2OutputResultAction)]
        public OutArgument<String> OutputResultAction { get; set; }

        #endregion

        protected string CleanupFetchXml(int? queryType, string fetchXml)
        {
            // filter out "order by" clause when type is filter or template
            if (queryType == (int)SavedQueryQueryTypeEnum.OfflineFilters ||
                queryType == (int)SavedQueryQueryTypeEnum.OutlookFilters ||
                queryType == (int)SavedQueryQueryTypeEnum.OutlookTemplate ||
                queryType == (int)SavedQueryQueryTypeEnum.OfflineTemplate)
            {
                StringReader sr = new StringReader(fetchXml);

                using (XmlTextReader reader = new XmlTextReader(sr))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                    XmlNode node = xmlDoc.SelectSingleNode("/fetch/entity/order");
                    while (node != null)
                    {
                        node.ParentNode.RemoveChild(node);
                        node = xmlDoc.SelectSingleNode("/fetch/entity/order");
                    }

                    StringWriter result = new StringWriter();
                    using (XmlTextWriter writer = new XmlTextWriter(result))
                    {
                        xmlDoc.WriteTo(writer);
                    }

                    return result.GetStringBuilder().ToString();
                }
            }
            else
            {
                return fetchXml;
            }
        }

    }
}
